package com.csx.micrometer.invoke.config;

import com.csx.micrometer.invoke.core.outbound.loadbalance.MicrometerLoadBalancerOutboundInterceptor;
import com.csx.micrometer.invoke.custom.TagsCustom;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.client.loadbalancer.LoadBalancerAutoConfiguration;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequestFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author cuisongxu
 * @date 2022/12/3 23:12
 */
@AutoConfigureAfter(LoadBalancerAutoConfiguration.class)
@ConditionalOnClass({LoadBalancerClient.class, LoadBalancerRequestFactory.class, RestTemplate.class})
@Configuration
public class LoadBalancerOutBoundConfiguration {

    @ConditionalOnMissingBean
    @Bean(name = "micrometerLoadBalancerOutboundInterceptor")
    public LoadBalancerInterceptor micrometerLoadBalancerOutboundInterceptor(LoadBalancerClient loadBalancerClient, LoadBalancerRequestFactory loadBalancerRequestFactory, List<TagsCustom> tagsCustoms) {
        return new MicrometerLoadBalancerOutboundInterceptor(loadBalancerClient, loadBalancerRequestFactory, tagsCustoms);
    }
}
